<?PHP
  $template = new Template();
  $template->load("files");
  $template->assign_var("UPLOADURL","/admin/index.php?page=upload");
  $template->assign_var("DIR",htmlentities($_GET['dir']));
  $template->assign_var("NEWFOLDERURL","/admin/index.php?page=files&dir=".urlencode($_GET['dir']));
  
  $dir = $_GET['dir'];
  if($_POST['newFolder']){
    FileServer::createFolder("../content/uploads/".$dir,$_POST['name']);
  }
  if($_GET['unlink']){
    unlink("../content/uploads/".$_GET['dir']."/".$_GET['unlink']);
    $template->assign_var("MESSAGE",$language->getString("FILE")." ".htmlentities($_GET['unlink'])." ".$language->getString("WAS_DELETED"));
  }
  if($_GET['rmdir']){
    rmdir("../content/uploads/".$_GET['rmdir']);
    $template->assign_var("MESSAGE",$language->getString("DIR")." ".htmlentities($_GET['rmdir'])." ".$language->getString("WAS_DELETED"));
  }
  $verzeichnis = openDir("../content/uploads/".$dir);
  $pre = "";
  foreach(split("/",$dir) as $cDir){
    $index = $template->add_loop_item("PATH");
    $path .= "/".$cDir;
    if($path == "/"){
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=files");
      $template->assign_loop_var("PATH", $index, "LABEL","/");
      $path = "";
    }
    else{
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=files&dir=".$path);
      $template->assign_loop_var("PATH", $index, "LABEL",$cDir);
    }
  }
  $subFolders = FileServer::getFolders("../content/uploads/".$dir);
  if($subFolders){
    foreach($subFolders as $folder){
      $index = $template->add_loop_item("DIRS");
	  $template->assign_loop_var("DIRS",$index,"URL","/admin/index.php?page=files&dir=".$dir."/".$folder);
	  $template->assign_loop_var("DIRS",$index,"LABEL",$folder);
	}
  }
  if(trim($_GET['dir']) != "" & trim($_GET['dir']) != "/"){
    $template->assign_var("DELETEFOLDERLINK","<a href=\"/admin/index.php?page=files&rmdir=".$_GET['dir']."\">Ordner l&ouml;schen</a>");
  }
  else{
    $template->assign_var("DELETEFOLDERLINK","");
  }
  $files = FileServer::getFiles("../content/uploads/".$dir);
  if($files){
    $template->show_if("FILES_EXIST",true);
    $template->show_if("NOFILES",false);
    foreach($files as $file){
      $index = $template->add_loop_item("FILES");
      $path_info = pathinfo("../content/uploads/".$dir."/".$file);
      if(strtolower($path_info['extension'] == 'jpg') or
         strtolower($path_info['extension'] == 'jpeg') or
         strtolower($path_info['extension'] == 'gif') or
         strtolower($path_info['extension'] == 'png') or
         strtolower($path_info['extension'] == 'bmp')){
         $template->assign_loop_var("FILES",$index,"IMAGE","<img src=\"/content/uploads/".$dir."/".$file."\" style=\"max-width:100px;max-height:100px;\" />");
      }
      else{
         $template->assign_loop_var("FILES",$index,"IMAGE","");
      }
      $template->assign_loop_var("FILES",$index,"FILELINK","<a href=\"../content/uploads".$dir."/".$file."\">".$file."</a>");
      $template->assign_loop_var("FILES",$index,"DELETEURL","/admin/index.php?page=files&dir=".urlencode($_GET['dir'])."&unlink=".urlencode($file));
    }
  }
  else{
    $template->show_if("FILES_EXIST",false);
    $template->show_if("NOFILES",true);
  }
  $template->assign_var("MESSAGE","");
  $template->output();
?>